// ANES20Economics

log using ANES20Economics.log, replace

use "C:\Users\sbstjp\OneDrive - Cardiff University\anes_timeseries_2020_stata_20220210.dta" // American National Election Study 2020 Time Series Study, Feb 10, 2022 Version. Date accessed: March 09, 2025. Accessed from https://electionstudies.org/data-center/2020-time-series-study/

// Social justice scale 
*Delete missing values
rename V201411x tgpolicy 
replace tgpolicy = . if tgpolicy == -2
rename V201626 offence 
replace offence = . if inlist(offence, -5, -9)
rename V202183 metoo 
replace metoo = . if inlist(metoo, -9, -7, -6, -5, -4, 998, 999)
rename V202174 blm 
replace blm = . if inlist(blm, -9, -7, -6, -5, -4, 998, 999)

*Reverse code offence so social justice values are high
egen maxval = max(offence)
gen roffence = maxval + 1 - offence
drop maxval

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in tgpolicy roffence metoo blm {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in stgpolicy sroffence smetoo sblm  {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in stgpolicy sroffence smetoo sblm  {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Economic variables 

*Rename
rename V201327x nateconbetterorworse 
rename V201333x unempbetterorworse 
rename V201502 persfinbetterorworse 
rename V201593 abilitypayhealth 
rename V201595 abilitypayhouse 
rename V201594 worriedaboutfinance 
rename V201596 anyonecloselostjob 
rename V202352 socialclass 
rename V202562 studentloan 
rename V202563 foodstampsother 
rename V202564 havepension  
rename V201617x income 


* Delete missing values
foreach var of varlist nateconbetterorworse unempbetterorworse persfinbetterorworse ///
                        studentloan foodstampsother havepension abilitypayhealth ///
                        abilitypayhouse worriedaboutfinance anyonecloselostjob ///
                        socialclass income {
    replace `var' = . if `var' < 0
}

gen unemployment = . 
replace unemployment = 1 if inlist(V201533x, 20, 40)
replace unemployment = 0 if V201533x >= 0 & (V201533x < 20)
replace unemployment = 0 if inlist(V201533x, 51, 61, 71, 81)

*Reverse code economic variables so that bad conditions are coded low
foreach var in nateconbetterorworse unempbetterorworse persfinbetterorworse havepension abilitypayhealth abilitypayhouse unemployment {
    replace `var' = . if `var' < 0
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

// Correlations
pwcorr SocJusValues rnateconbetterorworse runempbetterorworse rpersfinbetterorworse studentloan foodstampsother rhavepension rabilitypayhealth rabilitypayhouse worriedaboutfinance anyonecloselostjob socialclass runemployment income [aweight=V200010b], sig

log close

